/**
 * 工作处理
 */
Ext.define('BenBen.modules.project.operation.OperationWorkHandleWindow', {
	extend : 'Ext.window.Window',
	requires: [
	   'Ext.ux.RowExpander',
       'BenBen.modules.system.organization.UserSearchWindow'
    ],
	width : 640,
    height : 570,
    autoScroll : true,
	initComponent : function() {
        var panel = this;
        var describe = Ext.create('Ext.form.TextArea',{
            name: 'describe',
            labelAlign : 'right',
            allowBlank: false,
            fieldLabel: '工作描述',
            value : panel.record.get('describe')
        });
        
        var workSet = Ext.create('Ext.form.FieldSet', {
            title : '工作内容',
            layout : 'form',
            items : [describe]
        });
        
        var content = Ext.create('Ext.form.TextArea',{
            name: 'content',
            labelAlign : 'right',
            allowBlank: false,
            fieldLabel: '处理内容'
        });
        var handleTime = Ext.create('BenBen.common.component.datetime.DateTimeField',{
    		fieldLabel: '处理日期',
    		labelAlign : 'right',
    		allowBlank: false,
    		format:"Ymd",
    		value : new Date(),
    		name      :'handleTime'
    	});
    	var duration = Ext.create('Ext.form.NumberField',{
    		fieldLabel: '处理时长(分钟)',
    		labelAlign : 'right',
    		allowBlank: false,
    		name      :'duration'
    	});
    	var state = Ext.create('Ext.form.ComboBox',{
        	name: 'state',
        	fieldLabel: '处理状态',
        	allowBlank: false,
        	mode : 'local',
        	editable : false,
        	labelAlign : 'right',
        	triggerAction : 'all',
        	displayField : 'name',
        	valueField : 'number',
        	store : new Ext.data.SimpleStore({
        		fields : ['number','name'],
        		data : [[4,'完成'],[3,'挂起'],[1,'转移'],[2,'处理中']]
        	}),
        	listeners: {
                 select: function(combo, record, option) {
                	 if(combo.getValue() == 1){
                    	 shiftObject.allowBlank = false;
                     }else{
                    	 if(combo.getValue() == 2){
                    		 duration.allowBlank = true;
                    	 }
                    	 shiftObject.allowBlank = true;
                     }
                     
                     
                 }
             }
        });
    	var shiftObject = Ext.create('Ext.form.TextField',{
            fieldLabel: '转移对象',
            labelAlign : 'right',
            name:'trueName',
            listeners  : {  
                'focus':function(){  
                    var userWin =  Ext.create('BenBen.modules.system.organization.UserSearchWindow',{
                    	fromHandle : true,
                    	targetForm : form
                    });
                    
                    userWin.show();
                }  
            }  
        });
    	//这个是账号
    	var username = Ext.create('Ext.form.Hidden',{
            fieldLabel: '用户',
            name      :'username'
        });
        var handleSet = Ext.create('Ext.form.FieldSet', {
            title : '我的处理',
            layout : 'form',
            items : [content,{
            	autoHeight : true,
                layout : 'column',
                border : false,
                items : [{
                    columnWidth : .5,
                    layout : 'form',
                    border : false,
                    items : [handleTime,state]
                }, {
                    columnWidth : .5,
                    layout : 'form',
                    border : false,
                    items : [duration,shiftObject,username]
                }]
            }]
        });
        var form = Ext.create('Ext.form.Panel', {
        	frame : false,
        	border : false,
        	layout : 'form',
            defaults : {
                labelWidth : 60,
                msgTarget :'side',
                labelAlign : 'right'
            },
            items : [workSet,handleSet]
        });
        
        Ext.define('dataModel', {
			extend : 'Ext.data.Model',
			fields : ['id','handleTime', 'duration', 'state','upTrueName','trueName','content'],
			idProperty : 'id'
		});
        var store = new Ext.create('Ext.data.Store', {
			model : 'dataModel',
			remoteSort : true,
			proxy: {
		        type: 'rest',
		        actionMethods: {
	                create : 'POST',
	                read   : 'GET',
	                update : 'POST',
	                destroy: 'POST'
	            },
                url : 'project/operationWork/handles',
		        reader: {
		            type: 'json',
		            root: 'data',
		            totalProperty: 'totalCount'
		        }
		     }
		});
        store.load({
        	params : {
        		workId : panel.record.get('id')
        	}
        });
        var grid = Ext.create('Ext.grid.Panel', {
            store: store,
            columns: [{
                dataIndex: 'trueName',
                text: '处理人'
            },{
                dataIndex: 'upTrueName',
                text: '上级处理人'
            },{
                dataIndex: 'handleTime',
                text: '处理时间',
                width: 100
            },{
            	dataIndex: 'duration',
            	text: '处理时长'
            },{
            	dataIndex: 'state',
            	text: '状态',
            	renderer: function (value) {
            		if (value == '1') {
            			return "转移";
            		}else if (value == '2') {
            			return "处理中";
            		}else if (value == '3') {
            			return "挂起";
            		}else if (value == '4') {
            			return "已完成";
            		}
            	}
            }],
            width: 620,
            height: 200,
            plugins: [{
                ptype: 'rowexpander',
                rowBodyTpl : [
                    '<p><b>处理内容:</b> {content}</p>'
                ]
            }],
            collapsible: true,
            animCollapse: false,
            title: '处理步骤',
            iconCls: 'icon-grid'
        });
        
        var bbar = new Ext.Toolbar({
            items : [{
                xtype: 'tbfill'
            },{
                text : '确定',
                iconCls : 'formsave',
                handler : function() {
                	form.submit({
                        clientValidation:true,
                        waitMsg:'请稍候',
                        waitTitle:'正在验证',
                        url: 'project/operationWork/handle',
                        params : {
                        	workId : panel.record.get('id')
                        },
                        method: 'POST',
                        success:function(form,action){
                        	showSuccess(action.result.info);
                            panel.store.load();
                            panel.hide();
                        },
                        failure:function(form,action){
                            showError(action.result.info);
                        }

                    });
                }
            }, {
                text : '取消',
                iconCls : 'formcancle',
                handler : function() {
                    panel.hide();
                }
            },{
                xtype: 'tbfill'
            }]
        });


		Ext.apply(this, {
			maximizable : true,
			closeAction : 'hide',
			layout : 'form',
            title : '工作处理',
            border : false,
            bbar : bbar,
			items : [{
				xtype : 'panel',
				layout : 'form',
				items : [form]
			},{
				xtype : 'panel',
				layout : 'form',
				items : [grid]
			}]
		});
		this.callParent(arguments);
	}
});